package cn.marui.lock;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.ReentrantLock;
/**
 * @author marui
 * @date 2020/1/6 14:58
 */
public class OrderNumGenerator {
    /**
     * 全局订单id
     */
    public  static int count = 0;
    private java.util.concurrent.locks.Lock lock = new ReentrantLock();


    /**
     * 以lock的方式解决
     */
    public  String getNumber() {
        try {
            lock.lock();
            SimpleDateFormat simpt = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            /*+"_"+Thread.currentThread().getId()*/
            return simpt.format(new Date()) + "-" + ++count;
        }finally {
            lock.unlock();
        }

    }

}


